﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class DuctAlignment
	{
		public void ductAlignment(Document doc, string alignmentType, bool isUserDate, Selection sel, double height)
		{
			if (alignmentType != "Reference")
			{
				if (!isUserDate)
				{
					HYDuctAlignment hyductAlignment = new HYDuctAlignment();
					if (alignmentType == "Top")
					{
						ISelectionFilter selectionFilter = new DuctSelectionFilter();
						Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择基准风管");
						Duct duct = doc.GetElement(reference) as Duct;
						double high = new YJKPiping<Duct>(duct).GetHeightOrDiameter() / 2.0 + duct.LevelOffset;
						reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择起始风管");
						Duct ductStart = doc.GetElement(reference) as Duct;
						hyductAlignment.DuctBranchAlignment(ductStart, 1, high);
					}
					if (alignmentType == "Middle")
					{
						ISelectionFilter selectionFilter2 = new DuctSelectionFilter();
						Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter2, "选择基准风管");
						Duct duct2 = doc.GetElement(reference) as Duct;
						new YJKPiping<Duct>(duct2);
						double levelOffset = duct2.LevelOffset;
						reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter2, "选择起始风管");
						Duct ductStart2 = doc.GetElement(reference) as Duct;
						hyductAlignment.DuctBranchAlignment(ductStart2, 2, levelOffset);
					}
					if (alignmentType == "Bottom")
					{
						ISelectionFilter selectionFilter3 = new DuctSelectionFilter();
						Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter3, "选择基准风管");
						Duct duct3 = doc.GetElement(reference) as Duct;
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct3);
						double high2 = duct3.LevelOffset - hypiping.GetHeightOrDiameter() / 2.0;
						reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter3, "选择起始风管");
						Duct ductStart3 = doc.GetElement(reference) as Duct;
						hyductAlignment.DuctBranchAlignment(ductStart3, 3, high2);
					}
				}
				if (isUserDate)
				{
					HYDuctAlignment hyductAlignment2 = new HYDuctAlignment();
					if (alignmentType == "Top")
					{
						ISelectionFilter selectionFilter4 = new DuctSelectionFilter();
						Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter4, "选择起始风管");
						Duct ductStart4 = doc.GetElement(reference) as Duct;
						hyductAlignment2.DuctBranchAlignment(ductStart4, 1, height);
					}
					if (alignmentType == "Middle")
					{
						ISelectionFilter selectionFilter5 = new DuctSelectionFilter();
						Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter5, "选择起始风管");
						Duct ductStart5 = doc.GetElement(reference) as Duct;
						hyductAlignment2.DuctBranchAlignment(ductStart5, 2, height);
					}
					if (alignmentType == "Bottom")
					{
						ISelectionFilter selectionFilter6 = new DuctSelectionFilter();
						Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter6, "选择起始风管");
						Duct ductStart6 = doc.GetElement(reference) as Duct;
						hyductAlignment2.DuctBranchAlignment(ductStart6, 3, height);
					}
				}
			}
			if (alignmentType == "Reference")
			{
				ISelectionFilter selectionFilter7 = new DuctSelectionFilter();
				new ElementContainsLocationCurveFilter();
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Edge, selectionFilter7, "请选择一条风管的边线为基准参考线");
				XYZ globalPoint = reference.GlobalPoint;
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(doc.GetElement(reference) as Duct);
				XYZ xyz = (hypiping2.StartConnector.Origin - hypiping2.EndConnector.Origin).Normalize();
				Line line = new YJKLine(globalPoint, globalPoint + 1.0 * xyz).Line;
				reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Edge, selectionFilter7, "选择风管的一边进行对齐");
				Duct duct4 = doc.GetElement(reference) as Duct;
				XYZ globalPoint2 = reference.GlobalPoint;
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(duct4);
				XYZ xyz2 = (hypiping3.StartConnector.Origin - hypiping3.EndConnector.Origin).Normalize();
				Line line2 = new YJKLine(globalPoint, globalPoint + 1.0 * xyz2).Line;
				if (new YJKLine(line).IsParallel(line2))
				{
					new HYDuctAlignment().DuctBranchAlignment(duct4, line, globalPoint2);
					return;
				}
				YJKMessageBox.Show("参考线对齐，两边线必须平行才可以进行！");
			}
		}
	}
}
